<?php
namespace app\admin\controller;

use think\Db;
use think\Request;

class Order extends Base{

    /**
     * 产品订单列表
     */
    public function index(){
        $where['a.is_del'] = 0;
        $account = I('account','');
        $mobile = I('mobile','');
        $order_number = I('order_number','');
        $status = I('status',-1);
        //今日订单
        $start_1 = strtotime(date('Y-m-d',time()));
        $end_1  = $start_1 + 24*3600;
        $where_1['create_time'] = ['between',[$start_1,$end_1]];
        $where_1['is_del'] = 0;
        $order_1 = M('goods_order')->where($where_1)->sum('total_price');
        //本月订单
        $start_2 = strtotime(date('Y-m',time()));
        $end_2  = $start_2 + 30*24*3600;
        $where_2['create_time'] = ['between',[$start_2,$end_2]];
        $where_2['is_del'] = 0;
        $order_2 = M('goods_order')->where($where_2)->sum('total_price');
        //累计订单
        $where_3['is_del'] = 0;
        $order_3 = M('goods_order')->where($where_3)->sum('total_price');
        if(!empty($account)){
            $where['c.account'] = ['like',"%{$account}%"];
        }
        if(!empty($mobile)){
            $where['a.mobile'] = ['like',"%{$mobile}%"];
        }
        if(!empty($order_number)){
            $where['a.order_number'] = ['like',"%{$order_number}%"];
        }
        if($status != -1){
            $where['a.status'] = $status;
        }
        $export = I('export',0);
        if($export == 1){
            $list_a = M('goods_order a')
                ->join('users c','a.user_id=c.id','left')
                ->field('a.*,c.account')
                ->where($where)
                ->order('a.id desc')
                ->select();
            if(!empty($list_a)) {
                foreach ($list_a as $key => $value) {
                    $goods_list = M('goods_order_relation a')
                        ->join('goods b', 'b.id=a.goods_id', 'left')
                        ->field('a.*,b.name,b.price,b.images')
                        ->where('a.goods_order_id', $value['id'])
                        ->select();
                    $list_a[$key]['goods_list'] = $goods_list;
                }
            }
            $this->goods_export($list_a);
        }
        $list = M('goods_order a')
            ->join('users c','a.user_id=c.id','left')
            ->field('a.*,c.account')
            ->where($where)
            ->order('a.id desc')
            ->paginate('',false,page_param());
        $list_item = $list->items();
        if(!empty($list_item)){
            foreach ($list_item as $key=>$value){
                $goods_list = M('goods_order_relation a')
                    ->join('goods b','b.id=a.goods_id','left')
                    ->field('a.*,b.name,b.price,b.images')
                    ->where('a.goods_order_id',$value['id'])
                    ->select();
                $list_item[$key]['goods_list'] = $goods_list;
            }
        }
        $this->assign('list', $list);
        $this->assign('list_item', $list_item);
        $this->assign('order_1', $order_1);
        $this->assign('order_2', $order_2);
        $this->assign('order_3', $order_3);
        return $this->fetch();
    }
    //产品订单编辑
    public function index_edit(Request $request){
        if($request->isPost()){
            return $this->index_store($request);
        }
        $id = I('id',0);
        $list = M('goods_order a')
            ->join('users c','a.user_id=c.id','left')
            ->field('a.*,c.account')
            ->where('a.id',$id)
            ->find();
        $goods_list = M('goods_order_relation a')
            ->join('goods b','b.id=a.goods_id','left')
            ->field('a.*,b.name,b.price,b.images')
            ->where('a.goods_order_id',$list['id'])
            ->select();
        $list['goods_list'] = $goods_list;
        $this->assign('info',$list);
        return $this->fetch();
    }
    //产品订单删除
    public function index_del(){
        $params = input();
        $ids = explode(',', $params['id']);
        $title = db('goods_order')->where('id', 'in', $ids)->column('order_number');
        db('goods_order')->where('id', 'in', $ids)->update(['is_del'=>1]);
        add_sys_log('删除产品订单:'.implode(',',$title));
        return $this->ajaxReturn(0, '成功');
    }
    //产品订单编辑提交
    public function index_store(){
        $params = input();
        $result = $this->validate(
            $params,
            ['mobile'  => 'require|max:15',
                'address'=>'require'
            ],
            ['mobile.require'=>'手机号码必填',
                'mobile.max'=>'手机号码最多15个字符',
                'address.require'=>'收货地址不能为空',
            ]
        );
        if(true !== $result){
            return $this->ajaxReturn(1, $result);
        }
        if($params['id']){
            db('goods_order')->where('id', $params['id'])->update($params);
            add_sys_log('修改产品订单:'.$params['order_number']);
        }
        return $this->ajaxReturn(0,"成功", url(CONTROLLER_NAME.'/index'));
    }

    //产品订单导出Excel
    public function goods_export($data){
        set_time_limit(0);
        $strTable = '<table width="500" border="1">';
        $strTable .= '<tr>';
        $strTable .= '<td style="text-align:center;font-size:12px;width:*">订单号</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;width:*">用户账号</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;width:*">联系电话</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">产品信息</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">订单金额</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">订单状态</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">添加时间</td>';
        $strTable .= '</tr>';
        if (!empty($data)) {
            foreach ($data as $key => $val) {
                if($val['status'] == 0){
                    $status = '待付款';
                }elseif( $val['status'] == 1){
                    $status = '待发货';
                }elseif( $val['status'] == 2){
                    $status = '待收货';
                }elseif( $val['status'] == 3){
                    $status = '已完成';
                }
                $goods_info = '';
                foreach ($val['goods_list'] as $k=>$v){
                    $goods_info .= '产品名称:'.$v['name'];
                    $goods_info .= '产品数量:'.$v['goods_num'];
                    $goods_info .= '产品型号:'.$v['goods_model'];
                    $goods_info .= '产品单价:'.$v['price'].'<br/>';
                }
                $strTable .= '<tr>';
                $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['order_number'] . '</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['account'] . '</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['mobile'] . '</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">' . $goods_info . '</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['total_price'] . '</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">' . $status . '</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">' . date('Y-m-d H:i',$val['create_time']) . '</td>';
                $strTable .= '</tr>';
            }
        }
        $strTable .= '</table>';
        unset($list);
        $name = '产品订单';
        downloadExcel($strTable, $name);
        exit();
    }
    /**
     * 课程订单列表
     */
    public function course_order(){
        $where['a.is_del'] = 0;
        $account = I('account','');
        $mobile = I('link_mobile','');
        $name = I('name','');
        $status = I('status',-1);
        //今日订单
        $start_1 = strtotime(date('Y-m-d',time()));
        $end_1  = $start_1 + 24*3600;
        $where_1['create_time'] = ['between',[$start_1,$end_1]];
        $where_1['is_del'] = 0;
        $order_1 = M('course_order')->where($where_1)->sum('total_price');
        //本月订单
        $start_2 = strtotime(date('Y-m',time()));
        $end_2  = $start_2 + 30*24*3600;
        $where_2['create_time'] = ['between',[$start_2,$end_2]];
        $where_2['is_del'] = 0;
        $order_2 = M('course_order')->where($where_2)->sum('total_price');
        //累计订单
        $where_3['is_del'] = 0;
        $order_3 = M('course_order')->where($where_3)->sum('total_price');
        if(!empty($account)){
            $where['c.account'] = ['like',"%{$account}%"];
        }
        if(!empty($mobile)){
            $where['a.link_mobile'] = ['like',"%{$mobile}%"];
        }
        if(!empty($name)){
            $where['b.name'] = ['like',"%{$name}%"];
        }
        if($status != -1){
            $where['a.status'] = $status;
        }
        $export = I('export',0);
        if($export == 1){
            $list_a = M('course_order a')
                ->join('course b','a.course_id=b.id','left')
                ->join('users c','a.user_id=c.id','left')
                ->field('a.*,b.name goods_name,b.course_time,c.account')
                ->where($where)
                ->order('a.id desc')
                ->select();
            $this->course_export($list_a);
        }
        $list = M('course_order a')
            ->join('course b','a.course_id=b.id','left')
            ->join('users c','a.user_id=c.id','left')
            ->field('a.*,b.name course_name,b.course_time,c.account')
            ->where($where)
            ->order('a.id desc')
            ->paginate('',false,page_param());
        $this->assign('list', $list);
        $this->assign('order_1', $order_1);
        $this->assign('order_2', $order_2);
        $this->assign('order_3', $order_3);
        return $this->fetch();
    }
    //课程订单编辑
    public function course_edit(Request $request){
        if($request->isPost()){
            return $this->course_store($request);
        }
        $id = I('id',0);
        $list = M('course_order a')
            ->join('course b','a.course_id=b.id','left')
            ->join('users c','a.user_id=c.id','left')
            ->field('a.*,b.name goods_name,b.course_time,c.account,c.mobile')
            ->where('a.id',$id)
            ->find();
        $this->assign('info',$list);
        return $this->fetch();
    }
    //课程订单删除
    public function course_del(){
        $params = input();
        $ids = explode(',', $params['id']);
        $title = db('course_order')->where('id', 'in', $ids)->column('order_number');
        db('course_order')->where('id', 'in', $ids)->update(['is_del'=>1]);
        add_sys_log('删除课程订单:'.implode(',',$title));
        return $this->ajaxReturn(0, '成功');
    }
    //课程订单编辑提交
    public function course_store(){
        $params = input();
        $result = $this->validate(
            $params,
            ['link_mobile'  => 'require|max:15',
                'linkman'=>'require'
            ],
            ['link_mobile.require'=>'手机号码必填',
                'link_mobile.max'=>'手机号码最多15个字符',
                'linkman.require'=>'联系人不能为空',
            ]
        );
        if(true !== $result){
            return $this->ajaxReturn(1, $result);
        }
        if($params['id']){
            db('course_order')->where('id', $params['id'])->update($params);
            add_sys_log('修改课程订单:'.$params['order_number']);
        }
        return $this->ajaxReturn(0,"成功", url(CONTROLLER_NAME.'/course_order'));
    }

    //课程订单导出Excel
    public function course_export($data){
        set_time_limit(0);
        $strTable = '<table width="500" border="1">';
        $strTable .= '<tr>';
        $strTable .= '<td style="text-align:center;font-size:12px;width:*">订单号</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;width:*">用户账号</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;width:*">联系电话</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;width:*">联系人</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">课程名称</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">购买人数</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">订单金额</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">开课时间</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">添加时间</td>';
        $strTable .= '</tr>';
        if (!empty($data)) {
            foreach ($data as $key => $val) {
                $strTable .= '<tr>';
                $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['order_number'] . '</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['account'] . '</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['link_mobile'] . '</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['linkman'] . '</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['goods_name'] . '</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['people_sum'] . '</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['total_price'] . '</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">' . date('Y-m-d H:i',$val['course_time']) . '</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">' . date('Y-m-d H:i',$val['create_time']) . '</td>';
                $strTable .= '</tr>';
            }
        }
        $strTable .= '</table>';
        unset($list);
        $name = '课程订单';
        downloadExcel($strTable, $name);
        exit();
    }
    //商品订单发货
    public function order_send(){
        $id = I('id',0);
        if(empty($id)){
            return return_json(1,'订单id不能为空');
        }
        M('goods_order')->where('id',$id)->update(['status'=>2]);
        return return_json(0);
    }
    //商品订单批量发货
    public function index_send_all(){
        $id = I('id','');
        $ids = explode(',',$id);
        //查询订单状态
        $status_list = M('goods_order')->where('id','in',$ids)->column('status');
        if(in_array(2,$status_list)||in_array(3,$status_list)){
            return return_json(1,'订单状态有误');
        }
        Db::startTrans();
        try{
            M('goods_order')->where('id','in',$ids)->update(['status'=>2]);
            Db::commit();
            return $this->ajaxReturn(0,'批量发货成功');
        }catch (\Exception $e){
            Db::rollback();
            return return_json(1,$e->getMessage());
        }
    }



}